// Source : https://leetcode.com/problems/reverse-linked-list/
// Author : simba
// Date   : 2016-06-21

package main

import (
	"fmt"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func createList(intSlice []int) *ListNode {
	n := len(intSlice)
	var head *ListNode = nil

	for i := n; i > 0; i-- {
		val := intSlice[i-1]
		node := new(ListNode)
		node.Val = val
		node.Next = head
		head = node
	}
	return head
}

func printList(head *ListNode) {
	node := head
	for node != nil {
		fmt.Println(node.Val)
		node = node.Next
	}
}

func reverseList(head *ListNode) *ListNode {
	var reverse *ListNode = nil
	node := head
	for node != nil {
		next := node.Next
		node.Next = reverse
		reverse = node
		node = next
	}
	return reverse
}

func main() {
	a := []int{3, 3, 3, 2, 1}
	la := createList(a)
	fmt.Println("反转前:")
	printList(la)
	ln := reverseList(la)
	fmt.Println("反转后:")
	printList(ln)
}
